package com.corpgovernment.organization.controller;

import com.corpgovernment.api.organization.enums.SwitchFieldKeyEnum;
import com.corpgovernment.api.organization.model.switchinfo.*;
import com.corpgovernment.api.organization.utils.SwitchUtil;
import com.corpgovernment.common.apollo.OrganizationApollo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.common.enums.ExceptionCodeEnum;
import com.corpgovernment.common.operatelog.OperateLog;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.corpgovernment.organization.constant.OrganizationResponseCodeEnum;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import com.corpgovernment.organization.service.ISwitchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RequestMapping(value = "/switch")
@RestController
@Slf4j
public class SwitchController {
    @Autowired
    private ISwitchService switchService;
    @Autowired
    private OrganizationApollo organizationApollo;
    /**
     * 保存开关信息(standard)  前端 差旅属性使用
     *
     * @param request
     * @return
     */
    @OperateLog
    @RequestMapping(value = "/saveOrgSwitch")
    public JSONResult saveOrgSwitch(@RequestBody SaveSwitchInfoRequest request) {
        switchService.saveOrgSwitch(request);
        return JSONResult.ok();
    }

    /**
     * 获取所有开关信息(standard) 前端 差旅属性使用
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getTravelAttributeSwitch")
    public JSONResult<GetSwitchListResponse> getTravelAttributeSwitch(@RequestBody GetSwitchListRequest request) {
        GetSwitchListResponse response = switchService.getTravelAttributeSwitch(request);
        log.info("getTravelAttributeSwitch controller:{}", JsonUtils.toJsonString(response));
        return JSONResult.success(response);
    }

    /**
     * 获取所有开关信息(standard) 所有开关
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/allSwitch")
    public JSONResult<GetAllSwitchResponse> allSwitch(@RequestBody GetSwitchListRequest request) {
        GetAllSwitchResponse response = switchService.allSwitch(request);
        return JSONResult.success(response);
    }



    /**
     * 查询出差申请类型 前端使用
     * @param request
     * @return
     */
    @RequestMapping(value = "/getApplyTripType")
    public JSONResult<List<SwitchValueInfo>> getApplyTripType(@RequestBody GetSwitchListRequest request) {
        GetAllSwitchResponse response = switchService.allSwitch(request);
        List<SwitchValueInfo> switchList = new ArrayList<>();
        // 加上紧急预订的选项
        SwitchValueInfo urgent = new SwitchValueInfo();
        urgent.setValue(SwitchFieldKeyEnum.URGENT_APPLY_TYPE.getKey());
        urgent.setDesc(SwitchFieldKeyEnum.URGENT_APPLY_TYPE.getName());
        switchList.add(urgent);
        switchList.addAll(response.getApplyTripTypeList());

        return JSONResult.success(switchList);
    }


    @RequestMapping(value = "/saveOrgSwitchSoa")
    public JSONResult saveOrgSwitchSoa(@RequestBody SwitchInfoSoaVo<?> request) {
        if(request == null||StringUtils.isBlank(request.getSwitchKey())||request.getValue()==null){
            return JSONResult.errorMsg("入参不合法");
        }
        SwitchInfoSoa switchInfoSoa = SwitchUtil.convertSwitchInfoSoa(request);
        switchService.saveOrgSwitchSoa(switchInfoSoa);
        return JSONResult.ok();
    }
    /**
     * 获取开关信息(standard)
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getSwitchInfo")
    @Deprecated
    public <T>SwitchInfoSoaVo<T> getSwitchInfo(@RequestBody GetSwitchListRequest<T> request) {
        if (StringUtils.isBlank(request.getOrgId())) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.ORG_ID_NULL);
        }
        if (StringUtils.isBlank(request.getSwitchKey())) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.SWITCH_KEY_DONT_NULL);
        }
        if (request.getTClass()==null) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.TCLASS_IS_NULL);
        }
        if (StringUtils.isBlank(request.getSwitchKey())) {
            throw new CorpBusinessException(OrganizationResponseCodeEnum.SWITCH_KEY_DONT_NULL);
        }
        SwitchInfoSoa switchInfoSoa = switchService.getSwitchInfo(request);
        SwitchInfoSoaVo<T> switchInfoSoaVo = null;
        if(switchInfoSoa!=null){
            switchInfoSoaVo = SwitchUtil.convertSwitchInfoSoaVo(switchInfoSoa, request.getTClass());
        }
        return switchInfoSoaVo;
    }

    @RequestMapping(value = "/getUUID")
    public JSONResult<String> getUUID() {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return  JSONResult.success(uuid);
    }
    /**
     * 获取用户支付方式
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserPayInfo")
    public JSONResult<List<PayInfoResponse>> getUserPayInfo(@RequestBody @Validated PayInfoRequest request) {
        return JSONResult.success(switchService.getUserPayInfo(request));
    }

    /**
     * 根据公司ID判断是否属于该客户
     * 中电科（CETC） true
     * 其他 false
     * @param corpId
     * @return
     */
    @RequestMapping(value = "/getTenantSwitch")
    public JSONResult getTenantSwitch(String corpId){
        boolean isEnable = Boolean.FALSE;
        String[] tenantArr = new String[]{"cetc"};
        Map<String, List<String>> tenantAndCorpRelation = organizationApollo.getTenantAndCorpRelation();
        Iterator<Map.Entry<String, List<String>>> iterator = tenantAndCorpRelation.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, List<String>> next = iterator.next();
            List<String> value = next.getValue();
            //cetc = 中电科
            if (!CollectionUtils.isEmpty(value) && value.contains(corpId) && Arrays.stream(tenantArr).filter(item -> item.equals(next.getKey())).count() >0){
                isEnable = Boolean.TRUE;
            }
        }
        return JSONResult.success(isEnable);
    }

}
